﻿<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>选择接受人</title>
    <!--
    1, 退回界面。
    2，需要 FK_Flow, FK_Node,WorkID, FID.
    3, 调用方法 ReturnWork.htm?FK_Flow=001&FK_Node=103&WorkID=232&FID=23
    -->
    <meta http-equiv="Access-Control-Allow-Origin" content="*" />
    <link href="../Scripts/bootstrap/css/bootstrap.css" rel="stylesheet" />
    <link href="../Scripts/bootstrap/css/components-rounded.css" rel="stylesheet" />
    <link href="../Scripts/bootstrap/css/font-awesome.css" rel="stylesheet" />
    <link href="../Scripts/bootstrap/css/themes/default/style.min.css" rel="stylesheet" />
    <link href="../../DataUser/Style/ccbpm.css" rel="stylesheet" type="text/css" />
    <script type="text/javascript" src="../Scripts/bootstrap/js/jquery.min.js"></script>
    <script type="text/javascript" src="../Scripts/bootstrap/js/bootstrap.min.js"></script>
    <script type="text/javascript" src="../Scripts/commonYangYH.js"></script>
    <script src="../Scripts/QueryString.js" type="text/javascript"></script>
    <script src="../Scripts/bootstrap/js/jquery.cokie.min.js"></script>
    <script type="text/javascript" src="../Scripts/bootstrap/js/jstree.js"></script>
    <script type="text/javascript" src="../Scripts/bootstrap/js/jsTree.ddl.js"></script>
    <!-- 导入配置文件. -->
    <script type="text/javascript" src="../Scripts/config.js"></script>
    <script type="text/javascript" src="../Comm/Gener.js"></script>
    <style type="text/css">
        #TB_Doc {
            height: 132px;
            width: 433px;
        }
    </style>
    <script type="text/javascript">

        $(function () {

            InitTreeData();
            bindTest();

        });

        //确定 人员选择
        function SelectEmps() {

            GetSelectedEmpsReturnVal();

            //No  是返回到别的页面的值
            if (returnVal.No == undefined || returnVal.No == '') {
                alert("未选择人员");
            } else {
                window.parent.selectEmpsWindowClose("确定");
            }
        }

        //取消人员选择
        function Close() {
            if (window.parent != null && window.parent.selectEmpsWindowClose != null) {
                window.parent.selectEmpsWindowClose("取消");
            }
        }

        var pageData = { IsSelectMore: GetQueryString('IsSelectMore') != undefined && GetQueryString('IsSelectMore')=="1" ? true:false, FK_Dept: GetQueryString('FK_Dept') || '', ParentDept: '' };
        var returnVal = {};

        //初始化树  接收人
        function InitTreeData() {
            //$('#tree').css('display', 'block');
            $('#OrgJSTree').css('display', 'block');

            var url = '';

            //初始化Tree
            var plugins = [];
            if (pageData.IsSelectMore == true) { //多选
                plugins = [
                    "checkbox",
                    "state",
                    "conditionalselect"
                ];
            }
            //返回节点数组
            var initNodeData = function (data, fk_dept2) {

                data = eval('(' + data + ')');

                //console.log(data); //在ie下报错，调试完毕后删除他。

                var fk_dept = fk_dept2;//pageData.FK_Dept;
                var parentNo = undefined;
                var fkDeptObj = $.grep(data.Depts, function (value) {
                    return value.No == fk_dept;
                });
                if (fkDeptObj != undefined && fkDeptObj.length == 1) {
                    parentNo = fkDeptObj[0].ParentNo;
                }
                pageData.FK_Dept = parentNo;
                //本节点
                var instance = $('#OrgJSTree').jstree(true);
                var depts = $.grep(data.Depts, function (value) {
                    return value.ParentNo == parentNo;
                })

                //if (instance.)
                //子部门
                var childDepts = $.grep(data.Depts, function (value) {
                    return value.ParentNo == fk_dept;
                })
                //人员
                var emps = data.Emps;

                var itemArr = [];
                $.each(depts, function (i, dept) {
                    var deptNode = {
                        "text": dept.Name,
                        "id": dept.No,
                        "children": true,
                        "data": dept
                    };

                    if (dept.No == fk_dept) {  //是本部门时加载子部门和人员
                        var children = [];
                        if (childDepts.length > 0) {
                            $.each(childDepts, function (i, childDept) {
                                var childDeptNode = {
                                    "text": childDept.Name,
                                    "id": childDept.No,
                                    "children": true,
                                    "data": childDept
                                };

                                children.push(childDeptNode);
                            });
                        }
                        if (emps.length > 0) {
                            $.each(emps, function (i, emp) {
                                var empNode = {
                                    "text": emp.Name,
                                    "id": emp.No,
                                    "children": false,
                                    "data": emp,
                                    "icon": "jstree-file"
                                };
                                children.push(empNode);
                            });
                        }
                        //deptNode.children = children;
                        deptNode.data.childrenData = children;
                    }

                    itemArr.push(deptNode);
                });

                if (instance.get_node(fk_dept) == false) {
                    pageData.ParentDept = parentNo;
                }
                else {
                    itemArr = itemArr[0].data.childrenData;
                }
                return itemArr;
            };

            $('#OrgJSTree').jstree({
                'core': {

                    'data': function (node, cb) {

                       url = Handler + "?DoType=SelectEmps_Init&FK_Node=" + GetQueryString("FK_Node") + "&m=" + Math.random() + (pageData.FK_Dept != undefined ? "&FK_Dept=" + pageData.FK_Dept : '');
                       // alert(url);

                        //初始化根节点
                        if (node.id == "#") {


//                            var handler = new HttpHandler("BP.WF.HttpHandler.WF_WorkOpt");
//                            handler.AddPara("FK_Node", GetQueryString("FK_Node"));
//                            handler.AddPara("FK_Dept", pageData.FK_Dept);
//                            var data = handler.DoMethodReturnString("SelectEmps_Init");


                            $.ajax({
                                type: 'post',
                                async: true,
                                url: url,
                                data: {},
                                dataType: 'html',
                                success: function (data) {
                                    if (data.indexOf('err@') == 0) {
                                        alert(data);
                                        return;
                                    }

                                    var fk_dept2 = pageData.FK_Dept;
                                    cb(initNodeData(data, fk_dept2));
                                }
                            });
                        }
                        else {
                            if (node.data != undefined && node.data.childrenData != undefined && node.data.childrenData.length > 0) {
                                var fk_dept2 = node.id;
                                cb(node.data.childrenData, fk_dept2);
                            }
                            else {
                                pageData.FK_Dept = node.id;

                                var handler = new HttpHandler("BP.WF.HttpHandler.WF_WorkOpt");
                                handler.AddPara("FK_Node", GetQueryString("FK_Node"));
                                handler.AddPara("FK_Dept", node.id);
                                var data = handler.DoMethodReturnString("SelectEmps_Init");

                                if (data.indexOf('err@') == 0) {
                                    alert(data);
                                    return;
                                }

                                var fk_dept2 = node.id;
                                cb(initNodeData(data, fk_dept2));
                            }
                        }
                    }
                },
                "plugins": plugins
            }).on('changed.jstree', function (node, cb) {
                GetSelectedEmpsReturnVal();
            }).bind("loaded.jstree", function (event, data) {
                data.instance.open_all();
            });
        }
        //弹出树
        function bindTest() {
            $('#test').bind('click', function () {
                if ($('#OrgJSTree').css('display') == "none") {
                    $('#OrgJSTree').css('display', 'block');
                    var instance = $('#OrgJSTree').jstree(true);
                    if ($('#orgTxt').val() != undefined && $('#orgTxt').val() != '') {
                        instance.select_node($('#orgTxt').val());
                    }
                }
                else {
                    $('#OrgJSTree').css('display', 'none');
                }
            });

            $('#toPrevDept').bind('click', function () {
                if (pageData.ParentDept == 'null' || pageData.ParentDept == 0) {
                    alert("已到第一级机构");
                    return;
                }

                //判断最高级别.
                var user = new WebUser();
                if (user.CustomerNo == 'TianYe') {
                    if (user.FK_Dept.indexOf('18099') == 0) {
                        if (pageData.ParentDept.indexOf('18099') != 0) {
                            alert('已经是最高级别了.');
                            return;
                        }
                    }
                }

                pageData.FK_Dept = pageData.ParentDept;

                $('#OrgJSTree').jstree(true).refresh();
            });
        }
        //获取树选中的值
        function GetSelectedEmpsReturnVal() {
            
            var instance = $('#OrgJSTree').jstree(true);

            var selectedIdArr = instance.get_selected();
            var selectedNodes = [];
            var nameArrs = [];
            var noArr = [];
            var nameArr = [];
            $.each(selectedIdArr, function (i, selectedId) {
                selectedNodes.push(instance.get_node(selectedId));
            });

            $.each(selectedNodes, function (i, selectedNode) {
                if (selectedNodes[i].icon != true) {
                    nameArrs.push(selectedNode.data);
                }
            });

            if (nameArrs.length > 0) {
                for (var property in nameArrs[0]) {
                    returnVal[property] = [];
                }
            }
            $.each(nameArrs, function (i, selected) {
                for (var property in returnVal) {
                    returnVal[property].push(selected[property]);
                }
            });
            for (var property in nameArrs[0]) {
                returnVal[property] = returnVal[property].join(",");
            }
        }
    </script>
</head>
<body>
  <input type="button" onclick="Close()" value="取消"/><input type="button" value="上一级" id="toPrevDept" /><input type="button" onclick="SelectEmps()" value="确定" />

    <div id="OrgJSTree" class="tree-demo jstree jstree-1 jstree-default">
    </div>

    <input type="button" onclick="Close()" value="取消"/><input type="button" onclick="SelectEmps()" value="确定" />
 
</body>
</html>
